<?php

namespace backend\modules\rbac\models;

use Yii;
use mdm\admin\components\Configs;
use yii\helpers\Json;
use yii\rbac\Item;
use common\rbac\rules\PolicyRule;
use yii\helpers\ArrayHelper;
use common\helpers\Html;
use common\models\shop\ShopStore;

/**
 * This is the model class for table "tbl_auth_item".
 *
 * @author Misbahul D Munir <misbahuldmunir@gmail.com>
 * @since 1.0
 */
class AuthRole extends \mdm\admin\models\AuthItem {

    public $policy;
    public $store;

    /**
     * Initialize object
     * @param Item  $item
     * @param array $config
     */
    public function __construct($item = null, $config = []) {
        parent::__construct($item, $config);
        if ($this->data) {
            $this->attributes = Json::decode($this->data);
        }
    }

    /**
     * @inheritdoc
     */
    public function rules() {
        return ArrayHelper::merge(parent::rules(), [
            [['policy', 'store'], 'validatePolicy', 'skipOnEmpty' => false],
            [['data'], 'validateData', 'skipOnEmpty' => false],
        ]);
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels() {
        return ArrayHelper::merge(parent::attributeLabels(), [
            'policy' => '权限策略',
            'store' => '授权店铺',
        ]);
    }

    /**
     * Check for rule
     */
    public function validatePolicy($attribute) {
        if ($this->ruleName) {
            $data = $this->data ? Json::decode($this->data) : [];
            $value = $this->$attribute;
            if (!empty($value)) {
                ArrayHelper::setValue($data, $attribute, $value);
            } else {
                ArrayHelper::remove($data, $attribute);
            }
            $this->data = !empty($data) ? Json::encode($data) : '';
        } else {
            $this->data = '';
        }
    }

    /**
     * Check for rule data
     */
    public function validateData($attribute) {
        if ($this->ruleName && !$this->$attribute) {
            $this->addError('ruleName', '启用权限策略后必需给角色授权的权限策略设置内容！');
        }
    }

    public function getRuleNameList() {
        $name = $this->ruleName;
        $rules = Configs::authManager()->getRules();
        $list = [];
        foreach ($rules as $name => $rule) {
            if ($rule instanceof PolicyRule) {
                $list[$name] = $rule->label;
            }
        }
        return $list;
    }

    public function getPolicyList() {
        return [
            'name' => '管理'
        ];
    }

    public function getStoreList() {
        $stores = ShopStore::findActive()->all();
        return ArrayHelper::map($stores, 'id', 'name');
    }

    public function getPolicies() {
        return '11';
    }

    public function getRawData() {
        $raw = '';
        if (!empty($this->policy)) {
            $stores = $this->policyList;
            $items = [];
            foreach ($this->policy as $hotelid) {
                $hotel = ArrayHelper::getValue($stores, $hotelid);
                $hotel && $items[] = $hotel;
            }
            $raw .= Html::ul($items);
        }
        if (!empty($this->store)) {
            $stores = $this->storeList;
            $items = [];
            foreach ($this->store as $storeId) {
                $store = ArrayHelper::getValue($stores, $storeId);
                $store && $items[] = $store;
            }
            $raw .= Html::ul($items);
        }
//        if (!empty($this->shop_type)) {
//            $types = $this->shopTypeList;
//            $items = [];
//            foreach ($this->shop_type as $typeid) {
//                $type = ArrayHelper::getValue($types, $typeid);
//                $type && $items[] = $type;
//            }
//            if ($raw) {
//                $raw .= '<hr>';
//            }
//            $raw .= Html::ul($items);
//        }
        return $raw;
    }

}
